/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import mx.containers.Canvas;
	import mx.events.FlexEvent;
	
	/**
	 * 继承自Canvas，又封装了  onActive，onUnActive
	 * 
	 */	
	
	public class EDICanvas extends Canvas
	{
		private var _onActive:Function;
		private var _onUnActive:Function;
		private var _pageController:EDIPageController;
		public var caption:String;
		public var ds:EDIDataSource;
		public var htmlPlanArr:Array;
		private var htmlPlanVisible:Boolean=false
		
		private var params:Object;
		
		/**
		 * 构造函数，初始化变量
		 * 
		 */		
		public function EDICanvas()
		{
			super();
			this.setStyle("fontSize","12");
			htmlPlanArr=new Array();
			this.addEventListener(FlexEvent.INITIALIZE,initalizeHandler);
			params = new Object();
//			this.ds=EDIApplication(this.parentApplication).ds;
//			this.ds = ds;
		}
		
		private function initalizeHandler(event:FlexEvent):void{
			if (this.ds == null){
				this.ds=EDIApplication(this.parentApplication).ds;
			}
		} 
		
		/**
		 * 页面处于加载状态被调用的方法
		 * 页面激活时触发，对ds进行赋值
		 * 
		 */		

		public function actived():void
		{
			this.visible = true;
			if(this.ds==null)
			{
				this.ds=EDIApplication(this.parentApplication).ds;
			}
			
			setHtmlPlanVisible(true);
			
			if (this._onActive != null)
			{
				_onActive();
			}
		}
		
		/**
		 * 页面处于卸载状态时被调用的方法
		 * 页面被隐藏时执行
		 * 
		 */		
		public function unActived():void{
			this.visible = false;
			
			setHtmlPlanVisible(false);
			
			if (this._onUnActive !=null)
				_onUnActive();
		}
		
		/**
		 * 当页面被加载时，发生的回调。
		 * 设置页面激活时回调函数
		 * @param value
		 * 
		 */		

		public function set onActive(value:Function):void{
			this._onActive = value;
		}

		/**
		 * 当页面被卸载时，发生的回调。
		 * 设置页面被隐藏时的回调函数
		 * @param value
		 * 
		 */		
		public function set onUnActive(value:Function):void{
			this._onUnActive = value;
		}
		
		/**
		 *  当该页面添加到EDIPageController页面控制器内的时候，
		 *   EDIPageController将自己注册到该页面上，方便用户使用
		 * EDIApplication全局页面控制器,在EDIApplication内使用同一个页面控制器
		 * @param value
		 * 
		 */		

		public function set pageController(value:EDIPageController):void{
			this._pageController = value;
		}
		/**
		 * EDIApplication全局页面控制器,在EDIApplication内使用同一个页面控制器
		 * @return 
		 * 
		 */		
		public function get pageController():EDIPageController{
			return this._pageController;
		}

		
		/**
		 *  设置本页面内的EDIHtmlPlan对象的visible属性
		 * 页面内的EDIHtmlPlan是否显示
		 * @param value
		 * 
		 */		
		public function setHtmlPlanVisible(value:Boolean=true):void
		{
			htmlPlanVisible=value;
			for(var i:int=0;i<htmlPlanArr.length;i++)
			{
				(htmlPlanArr[i] as EDIHtmlPlan).visible=value;	
			}
		}
		
		/**
		 *  EDIHtmlPlan注册到本页面，通常在EDIHtmlPlan对象创建时自动执行该方法。
		 * 向EDIHtmlPlan列表中注册
		 * @param htmlPlan
		 * 
		 */		

		public function registerHTMLPlan(htmlPlan:EDIHtmlPlan):void
		{
			htmlPlan.visible=htmlPlanVisible;
			var i:int=0
			for(i=0;i<htmlPlanArr.length;i++)
			{
				if(htmlPlanArr[i]==htmlPlan)
				{
					break;
				}
			}
			if(i==htmlPlanArr.length)
			{
				htmlPlanArr.push(htmlPlan);
			}
		}
		
		public function addParam(key:String,value:Object):void{
			this.params[key] = value;
		}
		
		public function getParam(key:String):Object{
			return this.params[key];
		}
	}
}